package com.work.demo_01.mapper;

import com.work.demo_01.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {
    /**
     * 插入用户数据
     * @param user
     * @return 受影响的行数
     */
    @Insert("INSERT INTO user (username,password,email,createdTime,identity,state)" +
            " values" +
            " (#{username},#{password},#{email},#{createdTime},#{identity},#{state})")
    Integer insert(User user);

    /**
     * 根据用户名username查询用户信息
     * @param username
     * @return 用户信息
     */
    @Select("select * from user where username=#{username}")
    User findByUsername(String username);

    /**
     * 查询出所有用户数据
     * @return 用户信息列表
     */
    @Select("select * from user")
    List<User> findAllUser();

    /**
     * 根据查询条件获取用户信息（username，createdTime，state）
     * @param user
     * @return 用户信息列表
     */
    @Select("<script> " +
            "select *" +
            "from user " +
            "<where>" +
            "<if test=\"username != null and username != ''\"> username=#{username}</if> " +
            "<if test=\"createdTime != null and createdTime != ''\">and createdTime=#{createdTime}</if> " +
            "<if test=\"state != null and state != ''\">and state=#{state}</if> " +
            "</where> " +
            "</script>")
    List<User> getUser(User user);
}
